package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.ZtreeDto;
import com.ideal.manage.esms.bean.project.ProjectManage;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.ProjectManageRepository;
import com.ideal.manage.esms.repository.system.UserRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created By Dai On 2018/4/24
 * Remark: 工程配置 -> 项目管理 Service
 */
@Service
public class ProjectManageService {
    @Autowired
    private ProjectManageRepository projectManageRepository;

    @Autowired
    private UserRepository userRepository;

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 数据表
     *
     * @param pageNum
     * @return
     */
    public PageDto findAll(int pageNum, HttpServletRequest request) {
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        //增加删除标识的过滤
        SpecificationOperator isValid = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(isValid);
        MySpecification<ProjectManage> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 10, sort);
        Page<ProjectManage> page = projectManageRepository.findAll(mySpecifications, pageable);
        //设置PageDto
        List<ProjectManage> parameters = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameters);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 根据id 查找项目管理对象
     *
     * @return
     */
    public ProjectManage findById(String id) {
        return projectManageRepository.findOne(id);
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 保存
     *
     * @param id
     * @param name
     * @param ip
     * @param uploadCycle
     */
    public void saveProject(String id, String name, String ip, Long uploadCycle) {
        ProjectManage projectManage = new ProjectManage();
        //获取当前登录用户
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        User user = userRepository.findOne(shiroUser.getId());
        if (StringUtils.isNotBlank(id)) {
            projectManage = projectManageRepository.findOne(id);
        } else {
            projectManage.setCreateTime(new Date());
            projectManage.setCreateUser(user);
        }
        projectManage.setUpdateTime(new Date());
        projectManage.setName(name);
        projectManage.setIp(ip);
        projectManage.setUploadCycle(uploadCycle);
        projectManageRepository.save(projectManage);
    }

    /**
     * 工程配置 -> 对象管理 -> 项目管理 -> 删除
     *
     * @param ids
     * @return
     */
    public void deleteProject(String[] ids) {
        List<ProjectManage> projectManages = projectManageRepository.findAll(Arrays.asList(ids));
        projectManages.forEach(projectManage -> projectManage.setIsValid(0));
        projectManageRepository.save(projectManages);
    }

    /**
     * 用id找到对应的项目并生成ZtreeDto
     *
     * @param id
     * @return
     */
    public List findZtreeDtoById(String id) {
        ProjectManage projectManage = projectManageRepository.findOne(id);
        List list = new ArrayList<ZtreeDto>() {{
            add(new ZtreeDto(projectManage.getId(), null, projectManage.getName(), "0"));
        }};
        return list;
    }
}
